package com.one.blocks.rbac.manager;

import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.Cached;
import com.one.blocks.common.enums.EnableEnum;
import com.one.blocks.mvc.cache.RedisClearPrefix;
import com.one.blocks.mvc.manager.AbstractBatchExcelManager;
import com.one.blocks.rbac.domain.SysDictData;
import com.one.blocks.rbac.mapper.SysDictDataMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.one.blocks.rbac.constant.RbacConstant.DictDataCache;

/**
 * @author <a href="mailto:idler41@163.con">idler41</a> created on 2023-12-06 16:10:52
 */
@Service
public class SysDictDataManager extends AbstractBatchExcelManager<SysDictDataMapper, SysDictData> {

    @Autowired
    public SysDictDataManager(SysDictDataMapper mapper) {
        super(mapper);
    }

    @RedisClearPrefix(prefix = DictDataCache.CLEAR_PREFIX)
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insert(SysDictData entity) {
        super.insert(entity);
    }

    @RedisClearPrefix(prefix = DictDataCache.CLEAR_PREFIX)
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(Serializable id) {
        super.delete(id);
    }

    @RedisClearPrefix(prefix = DictDataCache.CLEAR_PREFIX)
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteByIds(Collection<? extends Serializable> ids) {
        super.deleteByIds(ids);
    }

    @RedisClearPrefix(prefix = DictDataCache.CLEAR_PREFIX)
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateById(SysDictData entity) {
        super.updateById(entity);
    }

    @RedisClearPrefix(prefix = DictDataCache.CLEAR_PREFIX)
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSelectiveById(SysDictData entity) {
        super.updateSelectiveById(entity);
    }

    // TODO SysDictData dictDataValue字段改为dictDataCode与dictDataType保持一致
    @Cached(area = DictDataCache.AREA, name = DictDataCache.NAME, key = DictDataCache.TYPE_CODE + "+args[0]", cacheType = CacheType.REMOTE)
    public Map<String, SysDictData> findAllByCode(String dictTypeCode) {
        // TODO 后续添加额外sql，比如根据排序字段排序
        SysDictData param = new SysDictData();
        param.setDictTypeCode(dictTypeCode);
        param.setState(EnableEnum.ENABLE.getCode());
        List<SysDictData> data = mapper.selectByEntity(param);
        data.sort(Comparator.comparing(SysDictData::getOrderNum).thenComparing(SysDictData::getId));
        return CollectionUtils.isEmpty(data) ? Collections.emptyMap() :
                data.stream().collect(Collectors.toMap(SysDictData::getDictDataValue, v -> v, (v1, v2) -> v1, LinkedHashMap::new));
    }
}